package cn.mastor.controller;

import cn.mastor.domain.Comment;
import cn.mastor.query.CommentQuery;
import cn.mastor.redisUtilService.RedisUtilService;
import cn.mastor.service.ICommentService;
import com.baomidou.mybatisplus.plugins.Page;
import mihoyo.result.JSONResult;
import mihoyo.result.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    RedisUtilService redisUtilsService;

    @Autowired
    public ICommentService commentService;

    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody Comment comment){
        if(comment.getId()!=null){
            commentService.updateById(comment);
        }else{
            commentService.insert(comment);
        }
        return JSONResult.success();
    }
    /**
     * 点赞
     */
    @RequestMapping(value = "/{id}/{type}/{commentId}",method = RequestMethod.GET)
    public JSONResult like(@PathVariable("id")Long id ,@PathVariable("type")Long type,@PathVariable("commentId")Long commentId){
        redisUtilsService.addOrConLike(id,type,commentId);
        return JSONResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
        commentService.deleteById(id);
        return JSONResult.success();
    }


    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(commentService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list(){
        return JSONResult.success(commentService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JSONResult page(@RequestBody CommentQuery query){
        Page<Comment> page = new Page<Comment>(query.getPage(),query.getRows());
        page = commentService.selectPage(page);
        return JSONResult.success(new PageList<Comment>(page.getTotal(),page.getRecords()));
    }
}
